package com.ai.alibaba.bailian.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.UUID;

/**
 * 阿里AI接口调用
 * @author houwenpeng
 */
@Service
public class AIChatService {

    private final ChatClient chatClient;

    private ChatMemory chatMemory = new InMemoryChatMemory();

    public AIChatService(ChatClient.Builder builder) {

//        this.chatClient = builder.build();

        this.chatClient = builder.defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory, UUID.randomUUID().toString(), 10)).build();
    }

    /**
     * 一次性出来
     * @param message
     * @return
     */
    public String chat(@Validated String message) {

        //System.out.println("提问 : " + message);

        String content = chatClient.prompt().user(message).call().content();

        //System.out.println("回答: "+content);

        return content;
    }

}
